package com.demo.进行工具类封装;

import java.sql.*;

public class Tool {//工具类都是私有构造器，静态方法，使用类名进行调用静态方法

    //私有构造器
    private Tool() { }
    //静态方法
    //注册驱动
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //获取数据库链接
    public static Connection getConnection() throws Exception{
        Connection con= DriverManager.getConnection("jdbc:mysql://localhost:3306/xclmf","root","552200");
        return con;
    }

    //关闭资源
    public static void close(Connection c, Statement p, ResultSet r){
        if (r != null) {
                try {
                    r.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (p != null) {
                try {
                    p.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (c != null) {
                try {
                    c.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
    }
}
